<template>
    <div class="register">
        <el-card class="card">
            <template #header>
                <h3>用户注册</h3>
            </template>

            <el-form :rules="rules" ref="registerForm" :model="user" label-width="auto">
                <el-form-item label="姓名" prop="name">
                    <el-input class="input" v-model="user.name" placeholder="请输入姓名" clearable />
                </el-form-item>
                <el-form-item label="账号" prop="account">
                    <el-input class="input" v-model="user.account" placeholder="请输入账号" clearable />
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                    <el-input class="input" v-model="user.phone" placeholder="请输入手机号" clearable />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input class="input" v-model="user.password" placeholder="请输入密码" show-password clearable />
                </el-form-item>
                <el-form-item label="确认密码" prop="PasswordConfirm">
                    <el-input class="input" v-model="user.PasswordConfirm" placeholder="请再次输入密码" show-password
                        clearable />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="footer">
                    <el-button class="register-btn" type="primary" @click="register">注册</el-button>
                    <el-text class="mx-1" type="info" @click="$router.push('/login')">已有账号？去登录</el-text>
                </div>
            </template>
        </el-card>
    </div>
</template>

<script>
import { ElMessage } from "element-plus";
import { registerAPI } from "@/api/user";
export default {
    data() {
        return {
            user: {
                name: '',
                account: '',
                phone: '',
                password: '',
                PasswordConfirm: '',
                role: 0
            },
            rules: {
                name: { required: true, message: '姓名不能为空', trigger: 'blur' },
                account: { required: true, message: '账号不能为空', trigger: 'blur' },
                phone: [
                    { required: true, message: '手机号不能为空', trigger: 'blur' },
                    {
                        validator: (rule, value, callback) => {
                            const phoneRegex = /^[0-9]{11}$/;
                            if (value !== '') {
                                if (phoneRegex.test(value)) {
                                    callback();
                                } else {
                                    callback(new Error('请输入有效的手机号'));
                                }
                            }
                        },
                        trigger: 'blur'
                    }
                ],
                password: { required: true, message: '请输入密码', trigger: 'blur' },
                PasswordConfirm: [
                    { required: true, message: '请再次输入密码', trigger: 'blur' },
                    {
                        validator: (rule, value, callback) => {
                            if (value !== this.user.password) {
                                callback(new Error('两次输入的密码不一致！'))
                            }
                            callback()
                        }, trigger: 'blur'
                    }
                ]
            }
        }
    },
    methods: {
        register() {
            this.$refs.registerForm.validate((val) => {
                if (val) {
                    registerAPI(this.user).then((resp) => {
                        if (resp.data.code == 2000) {
                            ElMessage.success(resp.data.message);
                            sessionStorage.setItem('user', JSON.stringify(resp.data.data));
                            this.$router.push('/');
                        } else {
                            ElMessage.error(resp.data.message);
                        }
                    })
                }
            });
        }
    }
}
</script>

<style scoped>
.register {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background-image: url("@/assets/imgs/居民登录注册背景图.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    background-position: 0px, 0px;
}

.register .card {
    width: 400px;
    opacity: 0.90;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

@media (max-width: 768px) {
    .register {
        background-position: center;
    }

    .register .card {
        width: 95vw;
        max-width: 400px;
    }
}

.register .card .input {
    width: 250px;
}

.footer {
    width: 300px;
}

.footer .register-btn {
    width: 100%;
}

.footer .mx-1 {
    cursor: pointer;

    &:hover {
        color: #409EFF;
    }
}
</style>
